package com.shteo.crmd.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.shteo.crmd.common.helper.StringHelper;
import com.shteo.crmd.common.source.AKResult;
import com.shteo.crmd.model.goods.Goods;
import com.shteo.crmd.web.api.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by Akuma on 16/9/28.
 */
@Api(value = "测试接口",description="用于测试商品的各种接口",tags = "/test")
@RestController
@RequestMapping("/test")
public class TestController {

    private Logger logger = Logger.getLogger(TestController.class);

    @Autowired
    private IGoodsService goodsService;


    @ApiOperation("添加商品")
    @PostMapping("/addGoods")
    public String addGoods(@ApiParam("商品")@RequestBody Goods goods){
        AKResult result = goodsService.add(goods);
        return result.build2();
    }


    @ApiOperation("获取所有商品")
    @GetMapping("/getGoods")
    public String getGoods(){
        List<Goods> goodsList = goodsService.getAllGoods();
        return StringHelper.toJSONString(JSONArray.toJSON(goodsList));
    }


    @ApiOperation("获取商品明细")
    @GetMapping("/getGoods/{goodsId}")
    public String getGoods(@ApiParam("商品ID")@PathVariable("goodsId")Long goodsId){
        Goods goods = goodsService.getGoodsById(goodsId);
        return StringHelper.toJSONString(goods);
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/delGoods/{goodsId}")
    public String delGoods(@ApiParam("商品ID")@PathVariable("goodsId")Long goodsId){
        return goodsService.delGoodsById(goodsId).build2();
    }

    @ApiOperation("更新商品")
    @PutMapping("/updateGoods")
    public String updateGoods(@ApiParam("商品")@RequestBody Goods goods){
        return goodsService.updateGoodsById(goods).build2();
    }
}
